cd "~/Dropbox/Education Realignment/Data/Analysis Files"
use "cumulative_2006-2020.dta", clear


label drop _all
gen BA = 0
replace BA=1 if educ>=5
replace pid7 =. if pid7==8
replace faminc =. if faminc==13
gen Union = 0
replace Union =1 if union_hh <=2
gen noOreligion = 0
replace noOreligion=1 if religion>=6
gen Catholic = 0
replace Catholic =1 if religion==2
gen Jewish = 0
replace Jewish = 1 if religion==5
gen black = 0
replace black = 1 if race==2
gen latino=0
replace latino=1 if race==3 | race==1 & hispanic==1
gen AsianO = 0
replace AsianO = 1 if race>=4
recode pid7 (1=7) (2=6) (3=5) (5=3) (6=2) (7=1)


asdoc regress pid7 gender age black latino AsianO Union faminc Catholic Jewish noOreligion i.year##i.BA [aw=weight_cum], replace nested dec(2) save(CES_PID)
margins, dydx(BA) at(year=(2008(1)2020)) predict(xb) saving(file1, replace) 
marginsplot, recast(scatter) horizontal title(CES Full Sample) ytitle(Year) xtitle(Effect of BA on PID) xline(0)
graph save "PID All", replace 
graph export "PID All.pdf", replace 

asdoc regress pid7 gender age Union faminc Catholic Jewish noOreligion i.year##i.BA [aw=weight_cum] if race==1, append nested dec(2) save(CES_PID)
margins, dydx(BA) at(year=(2008(1)2020)) predict(xb) saving(file2, replace) 
marginsplot, recast(scatter) horizontal title(CES Whites Only) ytitle(Year) xtitle(Effect of BA on PID) xline(0)
graph save "PID Whites", replace 
graph export "PID Whites.pdf", replace 

graph combine "PID All" "PID All ANES" "PID Whites" "PID Whites ANES", xcommon
graph export "PID Combined.pdf", replace 

regress pid7 gender age black latino AsianO Union faminc Catholic Jewish noOreligion i.year##c.educ [aw=weight_cum]
margins, dydx(educ) at(year=(2008(1)2020)) predict(xb) saving(file1, replace) 
marginsplot, recast(scatter) horizontal title(Full Sample) ytitle(Year) xtitle(Effect of BA on PID)
graph save "PID All", replace 
graph export "PID All.pdf", replace 

regress pid7 gender age Union faminc Catholic Jewish noOreligion i.year##c.educ [aw=weight_cum] if race==1 
margins, dydx(educ) at(year=(2008(1)2020)) predict(xb) saving(file2, replace) 
marginsplot, recast(scatter) horizontal title(Whites Only) ytitle(Year) xtitle(Effect of BA on PID)
graph save "PID Whites", replace 
graph export "PID Whites.pdf", replace 

gen Pres_Vote =.
replace Pres_Vote=1 if voted_pres_party==1
replace Pres_Vote=0 if voted_pres_party==2

asdoc logit Pres_Vote pid7 gender age black latino AsianO Union faminc Catholic Jewish noOreligion i.year##i.BA [pw=weight_cum], replace nested dec(2) save(CES_VC)
margins, dydx(BA) at(year=(2008(4)2020))  saving(file3, replace) 
marginsplot, recast(scatter) horizontal title(CES Full Sample) ytitle(Year) xtitle(Effect of BA on Dem Vote Choice) xline(0)
graph save "Pres Vote All", replace 

asdoc logit Pres_Vote pid7 gender age Union faminc Catholic Jewish noOreligion i.year##i.BA [pw=weight_cum] if race==1, append nested dec(2) save(CES_VC) 
margins, dydx(BA) at(year=(2008(4)2020)) saving(file4, replace) 
marginsplot, recast(scatter) horizontal title(CES Whites Only) ytitle(Year) xtitle(Effect of BA on Dem Vote Choice) xline(0)
graph save "Pres Vote Whites", replace 

graph combine "Pres Vote All" "PV All ANES" "Pres Vote Whites" "PV Whites ANES", xcommon
graph export  "Pres Vote Combined.pdf", replace 

gen House_Vote=.
replace House_Vote =1 if voted_rep_party==1
replace House_Vote =0 if voted_rep_party==2

logit House_Vote pid7 gender age black latino AsianO Union faminc Catholic Jewish noOreligion i.year##i.BA [pw=weight_cum] 
margins, dydx(BA) at(year=(2010(2)2018)) predict(xb) saving(file5, replace) 
marginsplot, recast(scatter) horizontal title(Full Sample) ytitle(Year) xtitle(Effect of BA on House Vote)
graph save "House Vote All", replace 

logit House_Vote pid7 gender age Union faminc Catholic Jewish noOreligion i.year##i.BA [pw=weight_cum] if race==1 
margins, dydx(BA) at(year=(2010(2)2018)) predict(xb) saving(file6, replace) 
marginsplot, recast(scatter) horizontal title(Whites Only) ytitle(Year) xtitle(Effect of BA on House Vote)
graph save "House Vote Whites", replace 

asdoc medeff (regress pid7 BA black latino AsianO Catholic Jewish noOreligion ) (regress Pres_Vote pid7  BA black latino AsianO Catholic Jewish noOreligion  ) if year==2008  , mediate(pid7) treat(BA) save(CES_Med) dec(2) replace 
asdoc medeff (regress pid7 BA black latino AsianO Catholic Jewish noOreligion  ) (regress Pres_Vote pid7  BA black latino AsianO Catholic Jewish noOreligion  ) if year==2012  , mediate(pid7) treat(BA) save(CES_Med) dec(2) append 
asdoc medeff (regress pid7 BA black latino AsianO Catholic Jewish noOreligion  ) (regress Pres_Vote pid7  BA black latino AsianO Catholic Jewish noOreligion  ) if year==2016  , mediate(pid7) treat(BA) save(CES_Med) dec(2) append 
asdoc medeff (regress pid7 BA black latino AsianO Catholic Jewish noOreligion  ) (regress Pres_Vote pid7  BA black latino AsianO Catholic Jewish noOreligion  ) if year==2020  , mediate(pid7) treat(BA) save(CES_Med) dec(2) append 


rename county_fips fips
sort fips year
merge m:1 fips year using "County Level for Merge.dta", force gen(co_merge)

asdoc regress pid7 gender age black latino AsianO Union faminc Catholic Jewish noOreligion i.year##i.BA c.BA_above_##i.BA [aw=weight_cum], save(CES_Int) dec(2) nested replace   
asdoc regress pid7 gender age Union faminc Catholic Jewish noOreligion i.year##i.BA c.BA_above_##i.BA [aw=weight_cum] if race==1, save(CES_Int) dec(2) nested append 
asdoc regress pid7 gender age Union faminc Catholic Jewish noOreligion i.year##i.BA c.BA_above_##i.BA [aw=weight_cum] if race~=1, save(CES_Int) dec(2) nested append 


asdoc regress pid7 gender age Union faminc Catholic Jewish noOreligion i.year##i.BA c.BA_above_##i.BA [aw=weight_cum] if race==1, save(CES_Int) dec(2) nested append 

outreg2 using edu_context, replace auto(2) dec(2) alpha(.05) symbol(*)
margins, dydx(BA) at(year=(2008(1)2019)) predict(xb) saving(file1a, replace) 
marginsplot, recast(scatter) horizontal title(Whites Only) ytitle(Year) xtitle(Effect of BA on PID)
graph save "PID Whites A", replace 

margins, dydx(BA) at(BA_above_=(10(1)80)) saving(file1b, replace) 
marginsplot, title(Whites Only) xtitle(County BA%) ytitle(Effect of BA on Party ID) recast(line)  recastci(rarea) ci1opts(color(gs7) fintensity(50)) xlabel(10(10)80)
graph save "PID Whites A", replace 
graph export "PID Interaction.pdf", replace 

asdoc regress pid7 gender age Union faminc Catholic Jewish noOreligion i.year##i.BA c.BA_above_##i.BA [aw=weight_cum], save(CES_Int) dec(2) nested append   
margins, dydx(BA) at(BA_above_=(10(1)80)) saving(file1c, replace) 
marginsplot, title(Whites Only) xtitle(County BA%) ytitle(Effect of BA on Party ID) recast(line)  recastci(rarea) ci1opts(color(gs7) fintensity(50)) xlabel(10(10)80)

asdoc regress pid7 gender age Union faminc Catholic Jewish noOreligion i.year##i.BA c.BA_above_##i.BA [aw=weight_cum] if race~=1, save(CES_Int) dec(2) nested replace   
outreg2 using edu_context, replace auto(2) dec(2) alpha(.05) symbol(*)
margins, dydx(BA) at(year=(2008(1)2019)) predict(xb) saving(file1a, replace) 
marginsplot, recast(scatter) horizontal title(Whites Only) ytitle(Year) xtitle(Effect of BA on PID)
graph save "PID N_Whites A", replace 


logit Pres_Vote pid7 gender age Union faminc Catholic Jewish noOreligion i.year##i.BA c.BA_above_##i.BA [pw=weight_cum] if race==1 
outreg2 using edu_context, append auto(2) dec(2) alpha(.05) symbol(*)
margins, dydx(BA) at(year=(2008(4)2016)) predict(xb) saving(file4, replace) 
marginsplot, recast(scatter) horizontal title(Whites Only) ytitle(Year) xtitle(Effect of BA on Pres. Vote)
graph save "Pres Vote Whites A", replace 
graph export "Vote Choice Interaction.pdf", replace 

margins, dydx(BA) at(BA_above_=(10(1)80)) saving(file1b, replace) 
marginsplot, title(Whites Only) xtitle(County BA%) ytitle(Effect of BA on Vote Choice) recast(line)  recastci(rarea) ci1opts(color(gs7) fintensity(50)) xlabel(10(10)80)
graph save "Pres Vote Whites B", replace 


histogram BA_above if year>=2001, by(year, note(Source: US Census Bureau)) xtitle(Percentage of County with a BA or Above) legend(off)
graph save "education histogram", replace
graph export "education histogram.pdf", replace 
